home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Source / MiscKit / MiscDictionary.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  3.3 KB  |  104 lines

  1. //
  2. // MiscDictionary.m -- HashTable subclass set up for unique string to
  3. //            object key/value pairs.  Access via HashTable methods.
  4. //        Written by Don Yacktman Copyright (c) 1994 by Don Yacktman.
  5. //                Version 1.0.  All rights reserved.
  6. //
  7. //        This notice may not be removed from this source code.
  8. //
  9. //    This object is included in the MiscKit by permission from the author
  10. //    and its use is governed by the MiscKit license, found in the file
  11. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  12. //    for a list of all applicable permissions and restrictions.
  13. //    
  14.  
  15. // This HashTable expects a MiscString as the key and any object as
  16. // the value.  Storage of keys is actually with NXAtoms, but all methods
  17. // with keys are overridden in order to hide this fact so you don't have
  18. // to deal with it.
  19.  
  20. #import <misckit/misckit.h>
  21.  
  22. // Initial capacity of new MiscDictionary objects
  23. #define MISCDICTCAP 512    // you may need this a lot larger.
  24. // BUG:  for some reason, the HashTable hasn't been automatically increasing
  25. // it's capacity, giving me segmentation faults instead.  Make sure that this
  26. // initial capacity is big enough to suit your needs...as long as it is big
  27. // enough, everything works as it should.  This shouldn't be at all needed,
  28. // since I should be able to init with cap. zero and have it grow as keys
  29. // are added, but it always crashes in this way, more or less:
  30. //        #0  0x50069cc in objc_msgSend ()
  31. //        #1  0x3fffae4 in ?? ()
  32. //        #2  0x500b214 in -[HashTable insertKey:value:] ()
  33. //        #3  0x110ca in -[MiscDictionary insertKey:value:] ()
  34. //        #4  0x5cba in -[MiscDictionary(TableParse) parseFromASCIIStream:] ()
  35. //        #5  0x5898 in MiscParseTableStream ()
  36. //        #6  0x57bc in MiscParseTableFile ()
  37. //        #7  0x31de in main () at test.m:7
  38.  
  39. @implementation MiscDictionary
  40.  
  41. - initKeyDesc: (const char *)aKeyDesc
  42. {
  43.     fprintf(stderr, "MiscDictionary -initKeyDesc: called.  Unexpected things may happen!\n");
  44.     return [self init];
  45. }
  46.  
  47. - initKeyDesc:(const char *)aKeyDesc valueDesc:(const char *)aValueDesc
  48. {
  49.     fprintf(stderr, "MiscDictionary -initKeyDesc:valueDesc: called.  Unexpected things may happen!\n");
  50.     return [self init];
  51. }
  52.  
  53. - initKeyDesc: (const char *) aKeyDesc valueDesc: (const char *) aValueDesc 
  54.     capacity: (unsigned) aCapacity
  55. {
  56.     fprintf(stderr, "MiscDictionary -initKeyDesc:valueDesc:capacity: called.\nUnexpected things may happen!\n");
  57.     return [self init];
  58. }
  59.  
  60. - initCapacity:(int)aCap
  61. {
  62.     return [super initKeyDesc:"%" valueDesc:"@" capacity:aCap];
  63. }
  64.  
  65. - init
  66. {
  67.     return [self initCapacity:MISCDICTCAP];
  68. }
  69.  
  70. - (BOOL)isKey:(const void *)aKey
  71. {
  72.     return [super isKey:[(id)aKey uniqueStringValue]];
  73. }
  74.  
  75. - (void *)valueForKey:(const void *)aKey
  76. {
  77.     return [super valueForKey:[(id)aKey uniqueStringValue]];
  78. }
  79.  
  80. - (void *)insertKey:(const void *)aKey value:(void *)aValue
  81. {
  82.     return [super insertKey:[(id)aKey uniqueStringValue] value:aValue];
  83. }
  84.  
  85. - (void *)removeKey:(const void *)aKey
  86. {
  87.     return [super removeKey:[(id)aKey uniqueStringValue]];
  88. }
  89.  
  90. - (BOOL)nextState:(NXHashState *)aState key:(const void **)aKey 
  91.     value:(void **)aValue
  92. { // the table is using NXAtoms, but we cover and return MiscStrings.
  93.     static id retString = nil;
  94.     static void *value;
  95.     const void *key;
  96.     BOOL ret = [super nextState:aState key:&key value:&value];
  97.     if (!retString) retString = [MiscString new];
  98.     [retString setStringValue:key];
  99.     *aValue = value;
  100.     *aKey = retString;
  101.     return ret;
  102. }
  103.  
  104. @end